package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiredPermission(name = "部门页面",expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/department/list";
    }

    @RequiredPermission(name="部门新增/编辑",expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Model model,Department dept){
        if (dept.getId()!=null){
            System.out.println(dept.toJson());
            departmentService.updateByPrimaryKey(dept);
        }else {
            departmentService.insert(dept);
        }
        return "redirect:/dept/list";
    }

    @RequiredPermission(name="部门删除",expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        departmentService.deleteByPrimaryKey(id);

        return "redirect:/dept/list";
    }
}

